﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.IO;

/// <summary>
///navigator 的摘要说明
/// </summary>
public static class navigator
{
    /// <summary>
    ///生成菜单
    /// </summary>
    /// <param name="role">用户角色，为不同角色生成不同菜单</param>
    /// <returns>返回菜单html代码</returns>
    public static string getMenu(string role,bool tag)
    {
        string navigatorStr = string.Empty;
        XmlDocument xml = new XmlDocument();
        xml.Load(AppDomain.CurrentDomain.BaseDirectory+"navigator.xml");
        XmlNodeList list = xml.SelectNodes(string.Format("//menuSys[@role='{0}']/menuItem", role));
        navigatorStr += "<ul class=\"treeview\" id=\"navigation\">";
        navigatorStr += generateMenu(list, string.Empty,1,tag);
        navigatorStr += "</ul>";
        return navigatorStr;
    }
    /// <summary>
    /// 创建无限级菜单
    /// </summary>
    /// <param name="list">同级菜单节点集合</param>
    /// <param name="navigatorStr">构建菜单字符串</param>
    /// <param name="role">角色</param>
    /// <returns></returns>
    static string generateMenu(XmlNodeList list, string navigatorStr,int i,bool tag)
    {
        foreach (XmlNode item in list)
        {
            if (tag)
            {
                if (item.SelectNodes("menuItem").Count > 0)
                {
                    navigatorStr += string.Format("<li ><span >   </span><a href='{0}?{2}' >{1}</a>", item.SelectSingleNode("url").InnerText, item.SelectSingleNode("name").InnerText, i >= 100 ? string.Format("{0}.{1}", (i / 100).ToString(), (i % 100).ToString()) : i.ToString());
                    navigatorStr += "<ul>";
                    navigatorStr += generateMenu(item.SelectNodes("menuItem"), string.Empty, i * 100,tag);
                    navigatorStr += "</ul></li>";
                }
                else
                {
                    navigatorStr += string.Format("<li><span > </span><a href='{0}?{2}'>{1}</a></li>", item.SelectSingleNode("url").InnerText, item.SelectSingleNode("name").InnerText, i >= 100 ? string.Format("{0}.{1}", (i / 100).ToString(), (i % 100).ToString()) : i.ToString());
                }
                i++;
            }
            else
            {
                if (item.SelectNodes("menuItem").Count > 0)
                {
                    navigatorStr += string.Format("<li ><span >   </span><a href='{0}?{2}' >{1}</a>", item.SelectSingleNode("url").InnerText!=""?string.Format("../{0}",item.SelectSingleNode("url").InnerText):"", item.SelectSingleNode("name").InnerText, i >= 100 ? string.Format("{0}.{1}", (i / 100).ToString(), (i % 100).ToString()) : i.ToString());
                    navigatorStr += "<ul>";
                    navigatorStr += generateMenu(item.SelectNodes("menuItem"), string.Empty, i * 100,tag);
                    navigatorStr += "</ul></li>";
                }
                else
                {
                    navigatorStr += string.Format("<li><span > </span><a href='{0}?{2}'>{1}</a></li>", item.SelectSingleNode("url").InnerText != "" ? string.Format("../{0}", item.SelectSingleNode("url").InnerText) : "", item.SelectSingleNode("name").InnerText, i >= 100 ? string.Format("{0}.{1}", (i / 100).ToString(), (i % 100).ToString()) : i.ToString());
                }
                i++;
            }
        }
        return navigatorStr;
    }
}
